//package com.yunda.nebula.gateway.filter;
//
//import com.yunda.monitor.client.MonitorClient;
//import com.yunda.monitor.common.AppMetricEnum;
//import com.yunda.monitor.metrics.AppMetric;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.SortedMap;
//
////import com.yunda.monitor.server.MonitorClient;
//
//@Slf4j
//@Service
//public class MonitorGatewayFilter implements GlobalFilter, Ordered {
//    MonitorClient client = MonitorClient.getClient();
//    public static final int LOAD_LOG_ORDER = 10001;
//    // 兼容银河系统
//    private static final List<String> YIN_HE_SYSTEM_PREFIX = new ArrayList<>();
//
//    static {
//        YIN_HE_SYSTEM_PREFIX.add("personnel");
//        YIN_HE_SYSTEM_PREFIX.add("blade-system ");
//    }
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        try {
//            //获取header的参数
//            ServerHttpRequest serverHttpRequest = exchange.getRequest();
//            String path = serverHttpRequest.getURI().getPath();
//            sendMetric(path);
//        } catch (Exception e) {
//            log.info("埋点异常,{}", e);
//        }
//        return chain.filter(exchange);
//    }
//
//    public void sendMetric(String path) {
//        if (StringUtils.isEmpty(path) || !path.startsWith("/") || !path.contains("/")) {
//            return;
//        }
//        String[] split = path.split("/");
//        if (null == split || split.length < 3) {
//            return;
//        }
//        StringBuilder bd = new StringBuilder("/");
//        if(YIN_HE_SYSTEM_PREFIX.contains(split[1])){
//            bd.append(split[1]);
//        }else {
//            bd.append(split[1])
//                    .append("/")
//                    .append(split[2]);
//        }
//        AppMetric userMetric = new AppMetric(AppMetricEnum.GATEWAY_APP_COUNT);
//        SortedMap<String, String> tags = userMetric.getTags();
//        tags.put("splitPath", bd.toString());
//        tags.put("path", path);
//        userMetric.getMetricUnit().setTags(tags);
//        client.sendCounterMetricData(userMetric);
//    }
//
//
//    @Override
//    public int getOrder() {
//        return LOAD_LOG_ORDER;
//    }
//}
